<?php


namespace Xy\Rbac\Repositories;


use Xy\Rbac\Models\RbacUserRoleModel;

class RbacUserRoleRepository extends BaseRepository {
    public $eloquentClassName = RbacUserRoleModel::class;
    
    public function getRoleByUserId($userId, $with = [], $select = ['*']) {
        return $this->getQuery()
            ->where('user_id', $userId)
            ->select($select)
            ->with($with)
            ->get();
    }
    
    /**
     * @param $userId
     *
     * @return mixed
     */
    public function delUserRoleRelativeByUserId($userId) {
        return $this->getQuery()
            ->where('user_id', $userId)
            ->delete();
    }
    
    /**
     * @param       $userId
     * @param array $roleIdList
     *
     * @return bool
     */
    public function assignRolesToUser($userId, array $roleIdList) {
        return $this->getQuery()->insert(array_map(function ($roleId) use ($userId) {
            return [
                'user_id' => $userId,
                'role_id' => $roleId,
            ];
        }, $roleIdList));
    }
}